public with sharing class CreateCaseController {

    public String caseid{get;set;}    
    public Case casecreate{get;set;}
    
    public Boolean Isinitialcall{get;set;}
    public Boolean Isappointment{get;set;}
    public Boolean Islodhalogin{get;set;}
    public Boolean Isbanklogin{get;set;}
    public Boolean Isinprocess{get;set;}
    public Boolean Issanction{get;set;}
    public Boolean Isadf{get;set;}
    public Boolean Isregistration{get;set;}
    public Boolean Isdemand{get;set;}
    public Boolean Isdisbursement{get;set;}
    public Boolean Iscustomerhold{get;set;} 
        
    public List<String> tempList {get;set;} 
    
    public String accountName {get; set;}
    public String accountId {get; set;}
    public Boolean IsNewCase {get;set;} 
    public Boolean IsAccCase {get;set;}  
    
    public CreateCaseController(ApexPages.StandardController controller) {
        
        casecreate=new Case();
        caseid=ApexPages.currentPage().getParameters().get('id');
        
        accountid=ApexPages.currentPage().getParameters().get('def_account_id');
        
        system.debug('accountid**********'+accountid);
         
        IsNewCase=false;
        IsAccCase=false;
        
        if(accountid!=null&&accountid!=''){
        
        Account acc=[select id,Name from Account where id=:accountid];
            accountName=acc.Name;
        
        system.debug('accountName**********'+accountName);
            
            IsAccCase=true;
        }else{
            IsNewCase=true;
        }
        
    }
    
    public pageReference CreateCase(){
             
             system.debug('accountid**********'+accountid);
             
        if(accountId!=null&&accountId!=''){
            
            system.debug('case.accountId**********: '+case.accountId);
            
            casecreate.AccountId=accountId;
            
            system.debug('casecreate**********: '+casecreate);
            
            Insert casecreate;
            
            system.debug('case.accountId: '+case.accountId);
            system.debug('casecreate.AccountId**********: '+casecreate.AccountId);
            system.debug('casecreate.id**********: '+casecreate.id);
            
            pageReference pageref=new pageReference('/apex/Casedetailpage?id='+casecreate.id);
            
            system.debug('casecreate.id**********: '+casecreate.id);
                   
            return pageref;
        }else{
              
              Insert casecreate;  
              
              system.debug('case.accountId: '+case.accountId);
              
              pageReference pageref=new pageReference('/apex/Casedetailpage?id='+casecreate.id);
              return pageref;
      }
         
    }
    
    

    public void Describepicklist(String fieldname){
    
        Schema.sObjectType sobject_type = Case.getSObjectType();
        Schema.DescribeSObjectResult sobject_describe =sobject_type.getDescribe(); 
        Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); 
        List<Schema.PicklistEntry> pick_list_values = field_map.get(fieldname).getDescribe().getPickListValues();
        tempList = new List<String>();
        
        for (Schema.PicklistEntry a : pick_list_values) {   
              
             tempList.add(a.getvalue());
             }
              
    }
    
    
    public void onchange(){
    
    try{
             
            Describepicklist('Loan_Stages__c');
            
                Isinitialcall=false;
                Isappointment=false;
                Islodhalogin=false;
                Isbanklogin=false;
                Isinprocess=false;
                Issanction=false;
                Isadf=false;
                Isregistration=false;
                Isdemand=false;
                Isdisbursement=false;
                Iscustomerhold=false;
                
            
            List<Boolean> boolenlist=new  List<Boolean>();
            
            boolenlist.add(false);
            boolenlist.add(false);
            boolenlist.add(false);
            boolenlist.add(false);
            boolenlist.add(false);
            boolenlist.add(false);
            boolenlist.add(false);
            boolenlist.add(false);
            boolenlist.add(false);
            boolenlist.add(false);
            boolenlist.add(false);
            
                                         
            if(casecreate.Loan_Stages__c!=null){
                String[] picklistvalues= casecreate.Loan_Stages__c.split(';');    
            if(picklistvalues!=null){   
            
                for(Integer i=0;i<tempList.size();i++) {                                                            
                   for(String pickvalue: picklistvalues){ 
                                 
                   if(pickvalue==tempList[i]){
                   
                         boolenlist[i]=true;
                        
                    }                   
                  }
               }
            }
          }
          
            Isinitialcall=boolenlist[0];
            Isappointment=boolenlist[1];
            Islodhalogin=boolenlist[2];
            Isbanklogin=boolenlist[3];
            Isinprocess=boolenlist[4]; 
            Issanction=boolenlist[5]; 
            Isadf=boolenlist[6];
            Isregistration=boolenlist[7];
            Isdemand=boolenlist[8];
            Isdisbursement=boolenlist[9];
            Iscustomerhold=boolenlist[10];
                        
                        
                               
         }catch(Exception e){
         
         }
            
       }
    
    public pageReference Cancel(){           
        pageReference pageref=new pageReference('/apex/PersonAccountHomeLoanDetail?id='+accountId);        
        return pageref;
    }
  
 
/************************************ Test Methods *************************************/    
      
    public static testmethod void TestCreateCaseController () {
    
        Account a=new Account();
        a.RecordTypeId=[Select id from RecordType where SobjectType='Account' AND Name='Booked Account' Limit 1].Id;
        a.FirstName='Test';
        a.LastName='Account';
        insert a;
                
        Account a1=new Account();
        a1.RecordTypeId=null;
        a1.FirstName='Test1';
        a1.LastName='Account1';
        
        
        
        ApexPages.StandardController controller = new ApexPages.StandardController(new Case());
        CreateCaseController casecreate0=new CreateCaseController (controller);
        
               
        casecreate0.casecreate=new Case();
        casecreate0.casecreate.Loan_Stages__c='Disbursement';
        casecreate0.Onchange();
        casecreate0.accountId=(String)a.Id;
        casecreate0.CreateCase();       
        casecreate0.Cancel();
        
        
        ApexPages.StandardController controller1 = new ApexPages.StandardController(new Case());        
        CreateCaseController casecreate1=new CreateCaseController (controller1);
        
        casecreate1.casecreate=new Case();
        casecreate1.casecreate.Loan_Stages__c='ADF';
        casecreate1.Onchange();
        casecreate1.CreateCase();       
        casecreate1.Cancel();
        
        ApexPages.StandardController controller2 = new ApexPages.StandardController(new Case());        
        CreateCaseController casecreate2=new CreateCaseController (controller2);
        casecreate2.Onchange();
    
    } 
 
}